package ws.jdbc.concurrent.driver.sqlserver

import java.sql.*

/**
 * Test forward-only and updatable result sets.
 *
 * @author Wilson Santos, 36228 <wilson.santos@ua.pt>
 */
class ForwardOnlyUpdatableTest extends ResultSetTest{

	/**
   * Test of updateRow() method.
   */
  void testUpdateRow(rs  = rs) {
    println "testUpdateRow()"

    5.times {
      assertTrue rs.next()
      assertLineValuesMatch(rs, it + 1)
    }

    rs.updateInt(1, 44)
    rs.updateString(2, "44")

    rs.updateRow()

    assertLineValuesMatch(rs, 5, 44, "44")

    rs.refreshRow()
    assertLineValuesMatch(rs, 5, 44, "44")

    45.times {
      assertTrue rs.next()
      assertLineValuesMatch(rs, 6 + it)
    }

    rs.updateInt(1, 12)
    rs.updateString(2, "12")

    rs.updateRow()

    assertLineValuesMatch(rs, 50, 12, "12")

    rs.refreshRow()
    assertLineValuesMatch(rs, 50, 12, "12")
  }

  /**
   * Test of insertRow() method.
   */
  void testInsertRow(rs = rs) {
    println "testInsertRow()"

    2.times {
      rs.next()
    }
    
    def curRow = rs.getRow()

    rs.moveToInsertRow()
    
    rs.updateInt(1, 54)
    rs.updateString(2, "65")

    rs.insertRow()

    rs.moveToCurrentRow()

    assertEquals curRow, rs.getRow()

    (DataTest.table.size() - 1).times {
      rs.next()
    }

    assertLineValuesMatch(rs, 51, 54, "65")
  }

  /**
   * Test of deleteRow() method.
   */
  void testDeleteRow() {
    println "testDeleteRow()"

    rs.next()
    rs.deleteRow()

    assertTrue rs.rowDeleted()

    rs.moveToInsertRow()
    try {
      rs.deleteRow()
      fail "Delete row should throw exception when on insert row."
    } catch(SQLException ex) {
      assertEquals Messages.msg_is_on_insert_row, ex.message
    }
  }
}